package club.adger.controller;

import club.adger.entity.Student;
import club.adger.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Collection;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Adger
 * @Date: 2020/08/27/15:42
 */
@RestController
@RequestMapping("/rest")
public class RESTHandler {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查找是发送get请求
     * @RequestMapping(value = "/findAll",method = RequestMethod.GET) 是等于 @GEtMapping的，就相当于简写形式吧
     * 由于中文放在了对象里面，会出现中文乱码
     * @return
     */
    @GetMapping("/findAll")
    public Collection<Student> findAll(HttpServletResponse response){
        response.setContentType("text/json;charset=UTF-8");
        return studentMapper.findAll();
    }

    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id") long id){
        return studentMapper.findById(id);
    }

    @PostMapping("/save")
    public void save(@RequestBody Student student) {
        studentMapper.saveOrUpdate(student);
    }

    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentMapper.saveOrUpdate(student);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        studentMapper.deleteById(id);
    }
}
